<template>
  <div class="mobile-login-wrapper">
    <div class="login-card">
      <h2 class="login-title">蚌埠凌空</h2>
      <p class="login-subtitle">请输入用户名和密码</p>

      <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-position="top"
          class="login-form"
          @submit.prevent="handleSubmit"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="ruleForm.username"
              placeholder="请输入用户名"
              @keyup.enter="handleSubmit"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
              v-model="ruleForm.password"
              type="password"
              placeholder="请输入密码"
              @keyup.enter="handleSubmit"
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              style="width: 100%"
              :loading="loading"
              @click="handleSubmit"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth.js'

const api = axios.create({
  baseURL: "http://project.lkkj.xyz",
  timeout: 5000,
})

const router = useRouter()
const authStore = useAuthStore()

const ruleFormRef = ref()
const loading = ref(false)

const ruleForm = reactive({
  username: '',
  password: ''
})

const rules = reactive({
  username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
})

const handleSubmit = async () => {
  if (!ruleFormRef.value) return

  try {
    await ruleFormRef.value.validate()
    loading.value = true

    const response = await api.post('/user/login', {
      username: ruleForm.username,
      password: ruleForm.password
    })

    const res = response.data

    if (res.code === 200) {
      ElMessage.success('登录成功，欢迎回来')
      authStore.setToken(res.data)
      await router.push('/project')
    } else {
      ElMessage.error(res.message || '登录失败，请检查用户名密码')
    }
  } catch (err) {
    console.error('登录异常', err)
    ElMessage.error('登录失败，服务器异常')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.mobile-login-wrapper {
  min-height: 100vh;
  padding: 24px;
  background-color: #f2f3f5;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.login-card {
  width: 100%;
  max-width: 360px;
  padding: 24px;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
}

.login-title {
  text-align: center;
  font-size: 22px;
  margin-bottom: 8px;
}

.login-subtitle {
  text-align: center;
  font-size: 14px;
  color: #888;
  margin-bottom: 24px;
}
</style>
